package com.aistar.controller;

import com.aistar.pojo.Choice;
import com.aistar.pojo.QuestionDetail;
import com.aistar.pojo.QuestionSet;
import com.aistar.pojoVO.QuestionDetailVO;
import com.aistar.service.ChoiceService;
import com.aistar.service.QuestionDetailService;
import com.aistar.service.QuestionSetService;
import com.aistar.serviceVO.QuestionDetailVOService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/answer")
public class ChoiceAnswerController {

    @Autowired
    private ChoiceService choiceService;

    @Autowired
    private QuestionDetailService questionDetailService;

    @Autowired
    private QuestionDetailVOService questionDetailVOService;

    @Autowired
    private QuestionSetService questionSetService;

    @RequestMapping("{questionSetId}/{choiceId}")
    public ModelAndView getAnswerAnalysis(@PathVariable("questionSetId") Integer questionSetId, @PathVariable("choiceId") Integer choiceId) {
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = questionDetailVOService.getByQuestionSetId(questionSetId);
        ServerResponse questionSetServerResponse = questionSetService.getById(questionSetId);
        if (MessageUtil.STATUS_SUCCESS.equals(serverResponse.getStatus())) {
            mav.addObject("questionDetailVOList", serverResponse.getData());
            List<QuestionDetailVO> questionDetailVOList = (List<QuestionDetailVO>) serverResponse.getData();
            for (int i = 0; i < questionDetailVOList.size(); i++) {
                QuestionDetailVO questionDetailVO = questionDetailVOList.get(i);
                if (choiceId.equals(questionDetailVO.getChoice().getChoiceId())) {
                    mav.addObject("questionDetailVO", questionDetailVO);
                    mav.addObject("countNum", i + 1);
                    break;
                }
            }
        }
        if (MessageUtil.STATUS_SUCCESS.equals(questionSetServerResponse.getStatus()))
            mav.addObject("questionSet", questionSetServerResponse.getData());
        mav.setViewName("choose/chooseResultAnalysis");
        return mav;
    }
}
